﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 字典项
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TDictItemController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        private readonly ITDictItemRepository dicitemR;

        public TDictItemController(ITDictItemRepository dicitemR, ILogRepository logRepository)
        {
            this.dicitemR = dicitemR;
            this.logRepository = logRepository;
        }
        /// <summary>
        /// 查询所有字典项
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllTDictItem(string? keyword, int typeId, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var res = dicitemR.GetAllTDictItem(keyword, typeId, pageIndex, pageSize, out var totalCount);
            ApiResult result = new ApiResult();
            result.code = "200";
            result.data = res;
            result.count = totalCount;
            return Ok(result);
        }
        /// <summary>
        /// 根据id查询字典项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTDictItemById(int id)
        {
            if(id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = dicitemR.GetTDictItemById(id);
            ApiResult result = new ApiResult();
            result.code = "200";
            result.data = res;
            return Ok(result);
        }
        /// <summary>
        /// 根据字典类型id查询字典项
        /// </summary>
        /// <param name="dicTypeId">字典类型id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTDictItemByDicTypeId(int dicTypeId, int pageIndex = 1, int pageSize = 5)
        {        
            var res = dicitemR.GetTDictItemByDicTypeId(dicTypeId, pageIndex, pageSize, out var totalCount);
            ApiResult result = new ApiResult();
            result.code = "200";
            result.data = res;
            result.count = totalCount;
            return Ok(result);
        }
        /// <summary>
        /// 字典项树
        /// </summary>
        /// <param name="itemName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTDictItemTreeByitemName(string? itemName,int typeId)
        {
            var res = dicitemR.GetTDictItemTreeByitemName(itemName, typeId);
            return Ok(BaseResult.Result(res, "", "200"));
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTDictItems()
        {
            return Ok(BaseResult.Result(dicitemR.GetDictItems(), "", "200"));
        }
        /// <summary>
        /// 添加字典项
        /// </summary>
        /// <param name="dictItem"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTDictItem(TDictItem dictItem)
        {
            var res = dicitemR.AddTDictItem(dictItem);
            ApiResult result = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了字典" + dictItem.ItemName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                result.code = "200";
                result.message = "添加成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "添加失败！";
            result.data = true;
            return Ok(result);
        }
        /// <summary>
        /// 修改字典项
        /// </summary>
        /// <param name="dictItem"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTDictItem(TDictItem dictItem)
        {
            if (dictItem.ItemName == "合格" || dictItem.ItemName == "不合格" || dictItem.ItemName == "待检")
            {
                return Ok(BaseResult.Result("400", dictItem.ItemName + "为系统状态不可更改！"));
            }
            if (dictItem.ItemName == "自产入库" || dictItem.ItemName == "外部调拨入库")
            {
                return Ok(BaseResult.Result("400", dictItem.ItemName + "为系统状态不可更改！"));
            }
            if (dictItem.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (dicitemR.GetAll().Any(d => d.ItemCode == dictItem.ItemCode && d.Id != dictItem.Id && d.TypeId==dictItem.TypeId))
                return Ok(BaseResult.Result(false, "项编码已存在", "500"));

            var res = dicitemR.UpdTDictItem(dictItem);
            ApiResult result = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了字典" + dictItem.ItemName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                result.code = "200";
                result.message = "修改成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "修改失败！";
            result.data = true;
            return Ok(result);
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id">字典id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTDictItem(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var dic=dicitemR.GetTDictItemById(id);
            if (dic!=null && (dic.ItemName == "合格" || dic.ItemName == "不合格" || dic.ItemName == "待检品"))
            {
                return Ok(BaseResult.Result("400", dic.ItemName + "为系统状态不可删除！"));
            }
            if (dic != null && (dic.ItemName == "自产入库" || dic.ItemName == "外部调拨入库"))
            {
                return Ok(BaseResult.Result("400", dic.ItemName + "为系统状态不可删除！"));
            }
            var res = dicitemR.DelTDictItem(id);
            ApiResult result = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了字典";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                result.code = "200";
                result.message = "删除成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "删除失败！";
            result.data = true;
            return Ok(result);
        }
        /// <summary>
        /// 批量删除字典项
        /// </summary>
        /// <param name="idList">字典id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTDictItemList(int[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            ApiResult result = new ApiResult();
            foreach (var id in idList)
            {
                var dic = dicitemR.GetTDictItemById(id);
                if (dic != null && (dic.ItemName == "合格" || dic.ItemName == "不合格" || dic.ItemName == "待检品"))
                {
                    return Ok(BaseResult.Result("400", dic.ItemName + "为系统状态不可删除！"));
                }
                var res = dicitemR.DelTDictItem(id);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = res ? uname + "删除了字典" : uname + "删除字典项失败";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logRepository.AddTLod(logg);
                if (!res)
                {
                    daRes = false;
                    break;
                }
            }

            if (daRes)
            {
                result.code = "200";
                result.message = "删除成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "删除失败！";
            result.data = true;
            return Ok(result);
        }

        /// <summary>
        /// 字典项树形展示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="typeid"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuLists(int pageIndex=1, int pageSize=10,int typeid=0,string? name="")
        {
            int totalCount;
            ApiResult apiResult = new ApiResult();
            var obj = dicitemR.GetMenuLists(pageIndex,pageSize,out totalCount,typeid, name);
            if (obj!=null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功";
                apiResult.count = totalCount;
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败";
            return Ok(apiResult);
        }
    }
}
